﻿namespace dogd
{
	/// <summary>
	/// Like is used by SqlStatementGenerator to provide search behaviour
	/// e.g. SELECT * FROM Episode WHERE (Name Like '43%') OR (Description Like '43%')
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Like", Justification = "As intended")]
	public class Like
	{
		private const string PercentText = "%";

		/// <summary>
		/// Initialize with columnName and search text
		/// </summary>
		/// <param name="name"></param>
		/// <param name="search"></param>
		public Like(string name, string search)
		{
			this.Name = name;

			if (search.Contains(PercentText))
			{
				this.Search = search;
			}
			else
			{
				this.Search = string.Format("{0}{1}{0}", PercentText, search);
			}
		}

		/// <summary>
		/// Column Name
		/// </summary>
		public string Name { get; set; }

		/// <summary>
		/// Search Text
		/// </summary>
		public string Search { get; set; }

		/// <summary>
		/// Override to provide sql text fragment
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return string.Format("{0} LIKE '{1}'", this.Name, this.Search);
		}
	}
}
